//
//  VRFirefightingDeviceCell.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/17.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRFirefightingDeviceCell.h"


#import "VRFirefightingDeviceModel.h"

@interface VRFirefightingDeviceCell()

@property (nonatomic, strong) UIView *containView;
@property (nonatomic, weak) UIImageView *iconImgView;
@property (nonatomic, weak) UILabel *nameLbl;
@property (nonatomic, weak) UILabel *typeNameLbl;
@property (nonatomic, weak) UILabel *noteTitleLbl;
@property (nonatomic, weak) UILabel *noteLbl;
@property (nonatomic, weak) UIButton *editBtn;
@property (nonatomic, weak) UISwitch *switchView;
@property (nonatomic, weak) UILabel *isCloseLbl;

@end

@implementation VRFirefightingDeviceCell

+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"VRFirefightingDeviceCell";
    
    VRFirefightingDeviceCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        
        cell = [[VRFirefightingDeviceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.accessoryType = UITableViewCellAccessoryNone;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        // lieview
        [self setupSubView];
        
    }
    return self;
}


- (void)setupSubView
{
    self.contentView.backgroundColor = ScrollViewBgColor;
    
    UIView *selectedBgView = [[UIView alloc] init];
    selectedBgView.backgroundColor = RGB(238, 238, 238);
    self.selectedBackgroundView = selectedBgView;
    
    UIView *containView = [[UIView alloc] init];
    containView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:containView];
    self.containView = containView;
    
    UIImageView *iconImgView = [[UIImageView alloc] init];
    iconImgView.contentMode = UIViewContentModeScaleAspectFit;
    [containView addSubview:iconImgView];
    self.iconImgView = iconImgView;
    
    UILabel *nameLbl = [[UILabel alloc] init];
    nameLbl.textColor = RGB(51, 51, 51);
    nameLbl.font = VRFont(14);
    [containView addSubview:nameLbl];
    self.nameLbl = nameLbl;
    
    UILabel *typeNameLbl = [[UILabel alloc] init];
    typeNameLbl.text = @"型号：";
    typeNameLbl.textColor = RGB(51, 51, 51);
    typeNameLbl.font = VRFont(14);
    [containView addSubview:typeNameLbl];
    self.typeNameLbl = typeNameLbl;
    
    UILabel *noteTitleLbl = [[UILabel alloc] init];
    noteTitleLbl.text = @"备注：";
    noteTitleLbl.textColor = MAINColor;
    noteTitleLbl.font = VRFont(14);
    [containView addSubview:noteTitleLbl];
    self.noteTitleLbl = noteTitleLbl;
    
    UILabel *noteLbl = [[UILabel alloc] init];
    noteLbl.numberOfLines = 2;
    noteLbl.text = @"备注";
    noteLbl.textColor = RGB(51, 51, 51);
    noteLbl.font = VRFont(14);
    [containView addSubview:noteLbl];
    self.noteLbl = noteLbl;
    
    UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    editBtn.titleLabel.font = VRFont(14);
    [editBtn setTitleColor:MAINColor forState:UIControlStateNormal];
    [editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [editBtn addTarget:self action:@selector(onEditBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [containView addSubview:editBtn];
    self.editBtn = editBtn;
    
    UISwitch *switchView = [[UISwitch alloc] init];
//    switchView.tintColor = RGB(78, 173, 57);
    [containView addSubview:switchView];
    [switchView addTarget:self action:@selector(onSwithClick:) forControlEvents:UIControlEventValueChanged];
    self.switchView = switchView;
    
    UILabel *isCloseLbl = [[UILabel alloc] init];
    isCloseLbl.text = @"关";
    isCloseLbl.textColor = RGB(51, 51, 51);
    isCloseLbl.font = VRFont(13);
    [containView addSubview:isCloseLbl];
    self.isCloseLbl = isCloseLbl;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
    }];
    
    [self.nameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImgView.mas_right).offset(10);
        make.top.mas_equalTo(18);
    }];
    
    [self.typeNameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImgView.mas_right).offset(10);
        make.centerY.mas_equalTo(self.containView);
    }];
    
    [self.noteTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImgView.mas_right).offset(10);
        make.bottom.mas_equalTo(-20);
    }];
    
    [self.noteLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.noteTitleLbl.mas_right).offset(0);
        make.centerY.mas_equalTo(self.noteTitleLbl);
        make.right.mas_equalTo(-120);
    }];
    
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-80);
        make.centerY.mas_equalTo(self.noteTitleLbl);
        make.width.mas_equalTo(36);
        make.height.mas_equalTo(30);
    }];
    
    [self.switchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-30);
        make.centerY.mas_equalTo(self.noteTitleLbl);
//        make.width.mas_equalTo(40);
//        make.height.mas_equalTo(24);
    }];
    
    [self.isCloseLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-8);
        make.centerY.mas_equalTo(self.noteTitleLbl);
    }];
}


#pragma mark - action
- (void)onEditBtnClick
{
    if (self.editClickBlock) {
        self.editClickBlock();
    }
}

- (void)onSwithClick:(UISwitch *)sender
{
    if (self.swithClickBlock) {
        self.swithClickBlock(sender.isOn);
    }
}


#pragma mark - setter && getter
- (void)setModel:(VRFirefightingDeviceModel *)model
{
    _model = model;
    
    NSDictionary *typeIconDict = @{@(0) : @"equipment_002", @(1) : @"equipment_003", @(3) : @"equipment_004", @(4) : @"equipment_006"};
    self.iconImgView.image = [UIImage imageNamed:typeIconDict[@(model.fType)]];
    self.nameLbl.text = model.fTypeName;
    self.typeNameLbl.text = [NSString stringWithFormat:@"型号：%@", model.fFacilityModelID];
    self.noteLbl.text = model.fAlias;
    self.switchView.on = model.fFacilitySwitch == 0;
    self.isCloseLbl.text = model.fFacilitySwitch == 0 ? @"开" : @"关";
    self.isCloseLbl.textColor = model.fFacilitySwitch == 0 ? RGB(78, 173, 53) : RGB(51, 51, 51);
//    self.nameLbl.text = model.fName;
    
    
}

@end
